/*
 * Selective Plugin
 * Author: Eugen Olshevsky (djakson)
 * Date: 03.11.2011
 */

(function($){
	$.fn.extend({
		selective: function(options){
			var storage = new Array();
			var defaultOptions = {
					countNums: 2,
					color: 'blue',
					defaultValue: '0',
					nums: [0,1,2,3,4,5,6,7,8,9],
					onSelectorClick: function(id, result){return true;},
					whenSelected: function(data){
						console.log(data);
					},
					updateNums: function(){}
			};
			if (typeof(options.countNums) == 'undefined') options.countNums = defaultOptions.countNums;
			if (typeof(options.defaultValue) == 'undefined') options.defaultValue = defaultOptions.defaultValue;
			if (typeof(options.onSelectorClick) == 'undefined') options.onSelectorClick = defaultOptions.onSelectorClick;
			var selectorClass = '<div class="selector">';
			for(i=0;i<options.countNums; i++){
				selectorClass += '<div class="dig" id="'+i+'"><div class="val" style="float:left;text-align:center;width:25px">'+options.defaultValue+'</div></div>';
			}
			selectorClass += '</div>'; 	
			$(selectorClass).insertBefore(this);
			$(this).remove();
			$('.dig').append('<div class="select"></div>');
			$this = this;
			$('.select').click(function(){
				$this.createMatrix(this);
				position = getAbsolutePosition(this);
				$('.number_matrix').css('left',position.x);
				$('.number_matrix').css('top',position.y+this.height);
				$('.number_matrix').attr('rel', $(this).parent('div').first().attr('id'));
				$('.number_matrix').show();
			});
			if (typeof(options.whenSelected)=='undefined') options.whenSelected = defaultOptions.whenSelected;
			if (typeof(options.updateNums)=='undefined') options.updateNums = defaultOptions.updateNums;
			this.createMatrix = function(selector){
				rel = $(selector).parent('.dig').attr('id');
				if($('.number_matrix').length!=0){
					$('.number_matrix').remove();
				}
				if (typeof(options.nums) == 'undefined') options.nums = defaultOptions.nums;
				var numberMatrix = '<div class="number_matrix" rel="'+rel+'">';
				$.each(options.nums, function(key,num){
					numberMatrix += '<div class="nitem">'+num+'</div>';
				});
				numberMatrix +='</div>';
				$('body').append(numberMatrix);
				$('.number_matrix').hover(null, function(){
					$('.number_matrix').fadeOut();
					$('.number_matrix').remove();
				});
				$('.nitem').click(function(){
					el_id = $(this).parent().first().attr('rel');
					$('.dig[id='+el_id+']').children('.val').text($(this).text());
					result = '';
					$.each($('.val'), function(k, v){
						result += $(v).text();
					});
					options.whenSelected(result);
				});
				result = '';
				$.each($('.val'), function(k, v){
					result += $(v).text();
				});
				options.onSelectorClick(rel, result);
				if (storage[rel]!=null){
					selective.setAvailableNums({digOrder:rel, nums: storage[rel]});
				}
			};
			
			this.setNum = function(num){
				if(typeof(num)!='string'){
					num = num.toString();
				};
				digs = $('.val').get().reverse();
				for(i=digs.length;i>=0;i--){
					if(typeof(num[i])!='undefined'){
						$(digs[i]).text(num[num.length-1-i]);  
					}
					else{
						$(digs[i]).text('0');
					}
				}
			};
			$this = this;
			this.setAvailableNums = function(opt){
				storage[opt.digOrder] = opt.nums;
				$('.nitem','.number_matrix[rel='+opt.digOrder+']').css('background-color', '#333333')
																  .css('color', '#FFFFFF')
																  .unbind('click')
																  .click(function(){
																	    el_id = $(this).parent().first().attr('rel');
																		$('.dig[id='+el_id+']').children('.val').text($(this).text());
																		result = '';
																		$.each($('.val'), function(k, v){
																			result += $(v).text();
																		});
																		options.whenSelected(result);
																  })
																  .hover(
																		  function(){
																			  $(this).css('background-color','#FFFFFF').css('color','#333333');
																		  },
																		  function(){
																			  $(this).css('background-color','#333333').css('color','#FFFFFF');
																		  }
																  );
				
				
				if (typeof($('.number_matrix')) != 'undefined') {
						$this = this;
						$.each($('.nitem','.number_matrix[rel='+opt.digOrder+']'), function(k,v){
							if($.inArray(parseInt(v.innerText), opt.nums) == -1){
								$(v).css('background-color', 'grey').css('cursor', 'default');
								$(v).unbind('click').unbind('mouseenter mouseleave');
							};
						});

				};
				return true;
			};
			
			return this;
		}
	});
	
})(jQuery);

function getAbsolutePosition(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}